package com.xjtu.conan.exception;


import com.xjtu.conan.response.AjaxResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice
@Slf4j
public class ExceptionHandle {

    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public AjaxResponse Handle(Exception e) {

        if (e instanceof MyException) {
            MyException myException = (MyException) e;
            log.info("[异常]{}", e.getMessage());
            return AjaxResponse.error(myException.getMessage());

        } else {
            //将系统异常以打印出来
            log.info("[系统异常]", e);
//            e.printStackTrace();
            return AjaxResponse.error(e.getMessage());
        }

    }
}
